/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdMetadataManagementSheet;
import com.comac.ins.isfd.domain.bo.IsfdMetadataManagementSheetBo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataManagementSheetVo;
import com.comac.ins.isfd.mapper.IsfdMetadataManagementSheetMapper;
import com.comac.ins.isfd.service.IIsfdMetadataManagementSheetService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 元数据管理-数据管理sheetService业务层处理
 *
 * @author Lion Li
 * @date 2024-09-23
 */
@RequiredArgsConstructor
@Service
public class IsfdMetadataManagementSheetServiceImpl implements IIsfdMetadataManagementSheetService {

    @Autowired
    private IsfdMetadataManagementSheetMapper baseMapper;

    /**
     * 查询元数据管理-数据管理sheet
     *
     * @param id 主键
     * @return 元数据管理-数据管理sheet
     */
    @Override
    public IsfdMetadataManagementSheetVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询元数据管理-数据管理sheet列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 元数据管理-数据管理sheet分页列表
     */
    @Override
    public TableDataInfo<IsfdMetadataManagementSheetVo> queryPageList(IsfdMetadataManagementSheetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdMetadataManagementSheet> lqw = buildQueryWrapper(bo);
        Page<IsfdMetadataManagementSheetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的元数据管理-数据管理sheet列表
     *
     * @param bo 查询条件
     * @return 元数据管理-数据管理sheet列表
     */
    @Override
    public List<IsfdMetadataManagementSheetVo> queryList(IsfdMetadataManagementSheetBo bo) {
        LambdaQueryWrapper<IsfdMetadataManagementSheet> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<IsfdMetadataManagementSheetVo> queryListByManagementIds(List<String> managementIds) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<IsfdMetadataManagementSheet>()
            .in(IsfdMetadataManagementSheet::getMetadataManagementId, managementIds));
    }

    private LambdaQueryWrapper<IsfdMetadataManagementSheet> buildQueryWrapper(IsfdMetadataManagementSheetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdMetadataManagementSheet> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMetadataManagementId() != null, IsfdMetadataManagementSheet::getMetadataManagementId, bo.getMetadataManagementId());
        lqw.like(StringUtils.isNotBlank(bo.getSheetName()), IsfdMetadataManagementSheet::getSheetName, bo.getSheetName());
        lqw.eq(StringUtils.isNotBlank(bo.getSheetUrl()), IsfdMetadataManagementSheet::getSheetUrl, bo.getSheetUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getMapFields()), IsfdMetadataManagementSheet::getMapFields, bo.getMapFields());
        lqw.eq(StringUtils.isNotBlank(bo.getTableName()), IsfdMetadataManagementSheet::getTableName, bo.getTableName());
        return lqw;
    }
}
